package com.sevenStar.healthManagement.controller;

import com.sevenStar.healthManagement.entity.User;
import com.sevenStar.healthManagement.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/users")
public class UserController {
	@Autowired
	private UserService userService;

	@GetMapping("/login")
	public String login() {
		return "userLogin";
	}

	@GetMapping("/register")
	public String register() {
		return "register";
	}

	@PostMapping("/register")
	public ModelAndView register(User user) {
		ModelAndView modelAndView = new ModelAndView();
		if (userService.queryPhoneOrIdNumber(user)) {
			modelAndView.addObject("errorMsg", "用户已存在!");
			modelAndView.setViewName("register.html");
		} else {
			if (userService.register(user)) {
				modelAndView.setViewName("redirect:login");
			} else {
				modelAndView.addObject("errorMsg", "注册失败!");
				modelAndView.setViewName("redirect:register");
			}
		}
		return modelAndView;
	}
}
